package com.rongda.deploy.operation.deploy.controller.body;

import com.rongda.deploy.operation.deploy.dto.R;
import com.rongda.deploy.operation.deploy.pojo.App;
import com.rongda.deploy.operation.deploy.service.DAppService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Create By WeiBin on 2020/10/26 11:16
 * 应用服务
 *
 * @author WB
 */
@RestController
@RequestMapping("/app")
@AllArgsConstructor
public class AppDeployController {

    private DAppService dAppService;

    @PostMapping
    public R insert(@RequestBody App app) {
        return new R<>(this.dAppService.insert(app));
    }

    @GetMapping
    public R list() {
        return new R<>(this.dAppService.queryAllByLimit(0, 100));
    }

    /**
     * 获取服务器下的所有应用
     *
     * @return 所有应用列表
     */
    @GetMapping("/list/{serverId}")
    public R getListByServerId(@PathVariable("serverId") Long serverId) {
        return new R<>(this.dAppService.queryAllByServerId(serverId));
    }

    /**
     * 查询单个app
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    public R getOneById(@PathVariable("id") Long id) {
        return new R<>(this.dAppService.queryById(id));
    }

    @PutMapping
    public R update(@RequestBody App app) {
        return new R<>(this.dAppService.update(app));
    }


    @DeleteMapping
    public R delete(Long id) {

        return new R<>(this.dAppService.deleteById(id));
    }

    @GetMapping("/reload/{id}")
    public R reloadApp(@PathVariable("id") Long id) {
        return new R<>(this.dAppService.reloadApp(id));
    }

    @GetMapping("/status/{id}")
    public R checkStatus(@PathVariable("id") Long id) {
        App app = this.dAppService.queryById(id);
        return new R<>(this.dAppService.checkAppStatus(app));
    }
}
